package com.cxj.musicplayer.activity;

import androidx.appcompat.app.AppCompatActivity;

import android.content.ComponentName;
import android.content.Intent;
import android.content.ServiceConnection;
import android.media.MediaPlayer;
import android.os.AsyncTask;
import android.os.Bundle;
import android.os.IBinder;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.Toast;

import com.cxj.musicplayer.MainActivity;
import com.cxj.musicplayer.R;
import com.cxj.musicplayer.adapter.SongListAdapter;
import com.cxj.musicplayer.audio.PlayerManager;
import com.cxj.musicplayer.data.ActivityManager;
import com.cxj.musicplayer.data.DataManager;
import com.cxj.musicplayer.musicsource.network.DownloadFrom163;
import com.cxj.musicplayer.service.MediaService;

import java.io.IOException;

public class SongsListActivity extends TitleActivity{

    private int position;
    ListView songs_list;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_songs_list);

        ActivityManager.activities.put(getClass(), this);

        position = getIntent().getIntExtra("position", -1);
        songs_list = findViewById(R.id.songs_list);

        songs_list.setOnItemClickListener(new ListView.OnItemClickListener(){
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {

                final String song_id = DataManager.array_data.get("n_song_id")[i].substring(DataManager.array_data.get("n_song_id")[i].lastIndexOf('=') + 1);
                Log.i(getLocalClassName() + ":song_id", song_id);
                Intent intent = new Intent(SongsListActivity.this, OnPlaying.class);
                intent.putExtra("song_id", song_id);
                intent.putExtra("song_i", i);
                intent.putExtra("song_source", "n_");
                ActivityManager.pre = SongsListActivity.class;
                startActivity(intent);

            }
        });

        try {
            String res = DownloadFrom163.get_songname_and_author_name(DataManager.array_data.get("play_list_id")[position]);
            songs_list.setAdapter(new SongListAdapter(this, res, DataManager.array_data.get("play_list_id")[position]));
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    @Override
    protected void onDestroy() {
        ActivityManager.activities.remove(getClass());
        super.onDestroy();
    }

}
